package com.uls.found.service.impl;

import java.util.List;

import com.uls.common.exception.ServiceException;
import com.uls.common.utils.DateUtils;
import com.uls.common.utils.StringUtils;
import com.uls.system.service.impl.SysUserServiceImpl;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.uls.found.mapper.EduLostFoundNoticeMapper;
import com.uls.found.domain.EduLostFoundNotice;
import com.uls.found.service.IEduLostFoundNoticeService;

/**
 * 寻物启事Service业务层处理
 * 
 * @author ruoyi
 * @date 2024-10-29
 */
@Service
public class EduLostFoundNoticeServiceImpl implements IEduLostFoundNoticeService 
{

    private static final Logger log = LoggerFactory.getLogger(SysUserServiceImpl.class);

    @Autowired
    private EduLostFoundNoticeMapper eduLostFoundNoticeMapper;

    /**
     * 查询寻物启事
     * 
     * @param noticeId 寻物启事主键
     * @return 寻物启事
     */
    @Override
    public EduLostFoundNotice selectEduLostFoundNoticeByNoticeId(Integer noticeId)
    {
        return eduLostFoundNoticeMapper.selectEduLostFoundNoticeByNoticeId(noticeId);
    }

    /**
     * 查询寻物启事列表
     * 
     * @param eduLostFoundNotice 寻物启事
     * @return 寻物启事
     */
    @Override
    public List<EduLostFoundNotice> selectEduLostFoundNoticeList(EduLostFoundNotice eduLostFoundNotice)
    {
        return eduLostFoundNoticeMapper.selectEduLostFoundNoticeList(eduLostFoundNotice);
    }

    /**
     * 新增寻物启事
     * 
     * @param eduLostFoundNotice 寻物启事
     * @return 结果
     */
    @Override
    public int insertEduLostFoundNotice(EduLostFoundNotice eduLostFoundNotice)
    {
        eduLostFoundNotice.setCreateTime(DateUtils.getNowDate());
        return eduLostFoundNoticeMapper.insertEduLostFoundNotice(eduLostFoundNotice);
    }

    /**
     * 修改寻物启事
     * 
     * @param eduLostFoundNotice 寻物启事
     * @return 结果
     */
    @Override
    public int updateEduLostFoundNotice(EduLostFoundNotice eduLostFoundNotice)
    {
        eduLostFoundNotice.setUpdateTime(DateUtils.getNowDate());
        return eduLostFoundNoticeMapper.updateEduLostFoundNotice(eduLostFoundNotice);
    }

    /**
     * 批量删除寻物启事
     * 
     * @param noticeIds 需要删除的寻物启事主键
     * @return 结果
     */
    @Override
    public int deleteEduLostFoundNoticeByNoticeIds(Integer[] noticeIds)
    {
        return eduLostFoundNoticeMapper.deleteEduLostFoundNoticeByNoticeIds(noticeIds);
    }

    /**
     * 删除寻物启事信息
     * 
     * @param noticeId 寻物启事主键
     * @return 结果
     */
    @Override
    public int deleteEduLostFoundNoticeByNoticeId(Integer noticeId)
    {
        return eduLostFoundNoticeMapper.deleteEduLostFoundNoticeByNoticeId(noticeId);
    }

    @Override
    public String importUser(List<EduLostFoundNotice> eduLostFoundNoticeList, boolean updateSupport) {

        if (StringUtils.isNull(eduLostFoundNoticeList) || eduLostFoundNoticeList.size() == 0)
        {
            throw new ServiceException("导入客户数据不能为空！");
        }
        int successNum = 0;
        int failureNum = 0;
        StringBuilder successMsg = new StringBuilder();
        StringBuilder failureMsg = new StringBuilder();

        for(EduLostFoundNotice eduLostFoundNotice:eduLostFoundNoticeList){
            try {

                eduLostFoundNoticeMapper.insertEduLostFoundNotice(eduLostFoundNotice);
                successNum++;
                successMsg.append("<br/>" + successNum + "、启事标题： " +eduLostFoundNotice.getNoticeTitle() + " 导入成功");
            } catch (Exception e)
            {
                failureNum++;
                String msg = "<br/>" + failureNum + "、启事标题： " +eduLostFoundNotice.getNoticeTitle() + " 导入失败：";
                failureMsg.append(msg + e.getMessage());
                log.error(msg, e);
            }
        }

        if (failureNum > 0)
        {
            failureMsg.insert(0, "很抱歉，导入失败！共 " + failureNum + " 条数据格式不正确，错误如下：");
            throw new ServiceException(failureMsg.toString());
        }
        else
        {
            successMsg.insert(0, "恭喜您，数据已全部导入成功！共 " + successNum + " 条，数据如下：");
        }
        return successMsg.toString();

    }
}
